using System;
using System.Linq;
using System.Threading;
using NUnit.Framework;
using SharpTestsEx;

namespace Limestone.Test
{
    [TestFixture]
    public class PageLookUpFixture : TestBase
    {

        [Test]
        public void PageStructureTest()
        {
            var pageProvider = GetPageProvider();

            /* wicked path */
            var index = new Page("A title") { Name = "home", Path = "/a/wicked/long/path/to/my/home" };
            pageProvider.Save(index);


            var lookup = GetStorage().GetPageLookUp(true);

            lookup.Pages.Count.Should().Be.EqualTo(8);
        }


        [Test]
        public void AddPageWithFunkyName()
        {
            var pageProvider = GetPageProvider();

            var page = new Page("A page title") { Name = "page", Path = "//page" };
            pageProvider.Save(page);

            page.Path.Should().Be.EqualTo("/page"); // remove excess slashes
        }


        [Test]
        public void ChildTest()
        {

            var pageProvider = GetPageProvider();

            /* mom */
            var mom = new Page("A title") { Name = "mom", Path = "/mom" };
            pageProvider.Save(mom);

            /* child */
            var child = new Page("A child title") { Name = "child", Path = "/mom/child" };
            pageProvider.Save(child);

            GetStorage().GetPageLookUp(true).Pages.Count.Should().Be.EqualTo(3); // 2 pages plus the root
        }

        [Test]
        public void DeleteTest()
        {
            var pageProvider = GetPageProvider();

            /* mom */
            var goner = new Page("A title") { Name = "goner", Path = "/goner" };
            pageProvider.Save(goner);
            pageProvider.Delete(goner);

            var store = GetStorage();
            var look = store.GetPageLookUp(false);

            var cache = GetPageStubCache();

            var rootStubs = cache.GetRootPageStubs(false);
            var cachedStub = cache.GetPageStub("/goner");

            Assert.AreEqual(1, look.Pages.Count);                           // 1 = the root
            Assert.AreEqual(1, rootStubs.Count());      // 1 = the root
            Assert.IsNull(cachedStub);
        }

        [Test]
        public void ManyTest()
        {

            var pageProvider = GetPageProvider();

            /* mom */
            var mom = new Page("A title") { Name = "mom", Path = "/mom" };
            pageProvider.Save(mom);

            /* child */
            var child = new Page("A child title") { Name = "child", Path = "/mom/child" };
            pageProvider.Save(child);
            var child2 = new Page("A child2 title") { Name = "child2", Path = "/mom/child2" };
            pageProvider.Save(child2);
            var child3 = new Page("A child3 title") { Name = "child3", Path = "/mom/child3" };
            pageProvider.Save(child3);

            /* dad */
            var dad = new Page("A title") { Name = "dad", Path = "/dad" };
            pageProvider.Save(dad);

            /* child */
            var child4 = new Page("A child title") { Name = "child4", Path = "/dad/child" };
            pageProvider.Save(child4);
            var child5 = new Page("A child2 title") { Name = "child5", Path = "/dad/child2" };
            pageProvider.Save(child5);
            var child6 = new Page("A child3 title") { Name = "child6", Path = "/dad/child3" };
            pageProvider.Save(child6);

            /* young dad */
            var youngDad = new Page("A title") { Name = "youngDad", Path = "/youngDad" };
            pageProvider.Save(youngDad);

            /* child */
            var grandChild = new Page("A grandChild title") { Name = "grandChild", Path = "/youngDad/grandChild" };
            pageProvider.Save(grandChild);

            var storage = GetStorage();
            var lookup = storage.GetPageLookUp(true);

            lookup.Pages.Count.Should().Be.EqualTo(11);
        }
    }
}